/*
 * iShopPlace Project.
 */
package ishopplace.view.bean;

import ishopplace.model.businessobject.Category;

import java.util.List;
import java.util.ArrayList;

import javax.faces.FacesException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * The managed bean with application scope. 
 * <p>
 * It is used as an application scope cache.
 * In JSF, the properties are set by bean management facility.
 * 
 * @author <a href="mailto:derek@derekshen.com">Derek Y. Shen</a>
 */
public class ApplicationBean extends BaseBean {
	//the logger for this class
	private Log logger = LogFactory.getLog(this.getClass());
	
	//cached category list
	private List categoryList;

	public ApplicationBean() {
		super();
		this.categoryList = new ArrayList();
		this.logger.debug("ApplicationBean is created");
	}
	
	/**
	 * Initializes the ApplicationBean.
	 * <p>
	 * Loads all the categories.
	 * 
	 * @see BaseBean#init()
	 */
	protected void init() {
		try {
			this.logger.debug("Initializes ApplicationBean");
			this.categoryList = this.getServiceLocator().getCatalogService().getCategoryList();
		} catch (Exception e) {
			String msg = "Could not initialize ApplicationBean " + e.toString();
			this.logger.error(msg);
			throw new FacesException(msg);
		}
		this.logger.debug("Application bean is initialized");
	}
	
	public List getCategoryList() {
		return this.categoryList;
	}
	
	public Category getCategory(String categoryId) {
		for (int i=0; i<this.categoryList.size(); i++) {
			Category category = (Category)this.categoryList.get(i);
			
			if (category.getCategoryId().equals(categoryId)) {
				return category;
			}
		}
		return null;
	}
}
